<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../include/tag.jsp" %>
<style type="text/css">
    .icon-close-new {
        width: 18px;
        height: 18px;
        background-image: url('${ctx}/image/form/create_bg.32.png');
        background-position: -522px 0px;
        position: absolute;
        top: -6px;
        right: -9px;
        cursor: pointer;
    }

    .tag-wrap {
        border: 1px solid #f1eeec;
        min-height: 100px;
        margin-top: 10px;
        padding: 5px;
    }

    .tag-wrap .tag-item {
        float: left;
        margin: 5px 10px 5px 5px;
        padding: 10px;
        background: #fff0f3;
        position: relative;
    }

    .tag-wrap .tag-name {
        line-height: 25px;
        display: inline-block;
        vertical-align: middle;
        float: left;
    }

    .tag-wrap .tag-item.active {
        color: #fff;
        background: #f4615c;
    }
</style>
<div id="setGiftDialog" style="display: none;">
    <form class="layui-form p10" method="post" action="">
        <a href="javascript:txz.giftUtil.selectGift();" class="layui-btn layui-btn-normal">选择礼包</a>
        <div class="tag-wrap">
            <%--<div class="tag-item" data-id="">
                <span class="tag-name">众筹成功礼包</span>
                <span name="event_form_item_ctrl" class="icon-close-new"></span>
            </div>
            <div class="tag-item" data-id="">
                <span class="tag-name">众筹成功礼包</span>
                <span name="event_form_item_ctrl" class="icon-close-new"></span>
            </div>
            <div class="tag-item" data-id="">
                <span class="tag-name">众筹成功礼包</span>
                <span name="event_form_item_ctrl" class="icon-close-new"></span>
            </div>
            <div class="tag-item" data-id="">
                <span class="tag-name">众筹成功礼包</span>
                <span name="event_form_item_ctrl" class="icon-close-new"></span>
            </div>
            <div class="cl"></div>--%>
        </div>
    </form>
</div>
<script type="text/javascript">
    $(function () {
        txz.giftUtil = {
            /**
             * 选择礼包
             * @targetType 项目类型
             */
            selectGift: function () {
                var _giftArray = new Array();
                $(".tag-wrap .tag-item").each(function (index, elem) {
                    var giftId = $(elem).data("id");
                    _giftArray.push(giftId);
                });
                var giftIds = _giftArray.join(",");
                var targetId = $(".tag-wrap").attr("targetId");
                var targetType = $(".tag-wrap").attr("targetType");
                openGiftDialog('选择礼包', '${ctx}/gift/gift/' + targetType + '/selectGift.do?targetId=' + targetId + '&giftIds=' + giftIds, '800px', '700px');

                function openGiftDialog(title, url, width, height, target) {
                    layer.open({
                        type: 2,
                        area: [width, height],
                        title: title,
                        maxmin: true, //开启最大化最小化按钮
                        content: url,
                        btn: ['确定', '关闭'],
                        yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            var inputForm = body.find('#inputForm');
                            var top_iframe;
                            if (target) {
                                top_iframe = target;//如果指定了iframe，则在改frame中跳转
                            } else {
                                top_iframe = '_parent';//获取当前active的tab的iframe
                            }
                            inputForm.attr("target", top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示

                            if (iframeWin.contentWindow.doSubmit()) {
                                var giftIds = $(iframeWin.contentWindow.$.find("#giftIds")).val();
                                var giftNames = $(iframeWin.contentWindow.$.find("#giftNames")).val();

                                var _idsArray = giftIds.split(",");
                                var _namesArray = giftNames.split(",");
                                var _contentArray = new Array();
                                for (var i = 0; i < _idsArray.length; i++) {
                                    _contentArray.push('<div class="tag-item" data-id="' + _idsArray[i] + '">');
                                    _contentArray.push('<span class="tag-name">' + _namesArray[i] + '</span>');
                                    _contentArray.push('<span name="event_form_item_ctrl" class="icon-close-new"></span>');
                                    _contentArray.push('</div>');
                                }
                                _contentArray.push('<div class="cl"></div>');
                                var $content = $(".tag-wrap");
                                $content.html("").append(_contentArray.join(""));

                                setTimeout(function () {
                                    top.layer.close(index);
                                }, 100);//延时0.1秒，对应360 7.1版本bug
                            }

                        },
                        cancel: function (index) {
                        }
                    });
                }
            },
            /**
             * 打开礼包设置弹层
             * @param targetId 项目id
             * @param targetName 项目名称
             */
            openSetGift: function (targetId, targetName, targetType) {
                $(".tag-wrap").attr("targetId", targetId);
                $(".tag-wrap").attr("targetType", targetType);
                //loading层
                var loadIndex = layer.load(1, {
                    shade: [0.1, '#fff'] //0.1透明度的白色背景
                });
                var setGiftIndex = layer.open({
                    type: 1,
                    area: '430px',
                    title: '配置礼包——' + targetName,
                    content: $("#setGiftDialog"),
                    btn: ['确定', '取消'],
                    success: function () { // 弹出后回调
                        $.post("${ctx}/gift/gift/ZC_ACTIVITY/findByTargetId.do", {
                            targetId: targetId
                        }, function (data) {
                            layer.close(loadIndex);
                            var _contentArray = new Array();
                            for (var i = 0; i < data.length; i++) {
                                _contentArray.push('<div class="tag-item" data-id="' + data[i].id + '">');
                                _contentArray.push('<span class="tag-name">' + data[i].name + '</span>');
                                _contentArray.push('<span name="event_form_item_ctrl" class="icon-close-new"></span>');
                                _contentArray.push('</div>');
                            }
                            if (_contentArray.length == 0) {
                                _contentArray.push('<div class="tag-item active">');
                                _contentArray.push('<span class="tag-name">暂时没有配置礼包</span>');
                                _contentArray.push('</div>');
                            }
                            _contentArray.push('<div class="cl"></div>');
                            var $content = $(".tag-wrap");
                            $content.html("").append(_contentArray.join(""));

                        })
                    },
                    end: function () { // 销毁后回调

                    },
                    yes: function () { // 确定按钮
                        var activeLength = $(".tag-wrap .active").length;
                        if (activeLength > 0) {
                            util.layerMsgError('还没有配置礼包不能保存');
                        } else {
                            var _giftArray = new Array();
                            $(".tag-wrap .tag-item").each(function (index, elem) {
                                var giftId = $(elem).data("id");
                                _giftArray.push(giftId);
                            });
                            var giftIds = _giftArray.join(",");

                            var selectGiftIndex = layer.load(1);
                            $.post("${ctx}/gift/gift/saveGiftTarget.do", {
                                targetId: targetId,
                                giftIds: giftIds
                            }, function (ret) {
                                layer.close(selectGiftIndex);
                                if (ret.success == true) {
                                    util.layerMsgSuccess("成功")
                                    layer.close(setGiftIndex);
                                } else {
                                    util.layerMsgError("失败")
                                }
                            });
                        }
                    }
                });

                $(".tag-wrap").delegate(".icon-close-new", 'click', function (e) {
                    var $target = $(e.target);
                    $target.closest(".tag-item").remove();
                })
            }
        }
    });
</script>